#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    vector<vector<int>> fourSum(vector<int>& nums, int target) {
        sort(nums.begin(), nums.end());

        int n = nums.size();
        vector<vector<int>> ret;

        for (int i = 0; i < n - 3; i++)
        {
            if (i != 0 && nums[i] == nums[i - 1])
                continue;
            for (int j = i + 1; j < n - 2; j++)
            {
                if (j != i + 1 && nums[j] == nums[j - 1])
                    continue;

                int left = j + 1, right = n - 1;
                while (left < right)
                {
                    long long sum = (long long)nums[i] + nums[j] + nums[left] + nums[right];

                    if (sum == target)
                    {
                        ret.push_back(vector<int>{nums[i], nums[j], nums[left], nums[right]});

                        while (left < right && nums[left] == nums[++left]);
                        while (left < right && nums[right] == nums[--right]);
                    }
                    else if (sum > target)
                        right--;
                    else
                        left++;
                }
            }
        }

        return ret;
    }
};